package com.usian.controller;

import cm.usian.feign.OrderFeign;
import com.usian.CartFeign;
import com.usian.pojo.OrderInfo;
import com.usian.pojo.TbItem;
import com.usian.pojo.TbOrder;
import com.usian.pojo.TbOrderShipping;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 订单服务 Controller
 */
@RestController
@RequestMapping("/frontend/order")
public class OrderController {
    @Autowired
    private CartFeign cartFeign;

    @Autowired
    private OrderFeign orderFeign;

    /**
     * 结算购物车
     * @param ids
     * @param userId
     * @return
     */
    @RequestMapping("/goSettlement")
    public Result goSettlement(String[] ids, String userId) {
        //先获取购物车
        Map<String, TbItem> cart = cartFeign.selectCartByUserId(userId);
        //从购物车中获取选中的商品
        List<TbItem> list=new ArrayList<TbItem>();
        for (String id : ids) {
            list.add(cart.get(id));
        }
        if (list.size()>0){
            return Result.ok(list);
        }
        return Result.error("error");
    }

    /**
     * 创建订单
     * @param orderItem
     * @param tbOrder
     * @param tbOrderShipping
     * @return
     */
    @RequestMapping("/insertOrder")
    public Result insertOrder(String orderItem, TbOrder tbOrder , TbOrderShipping tbOrderShipping){
        //因为一个request中只包含一个request body. 所以feign不支持多个@RequestBody。
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderItem(orderItem);
        orderInfo.setTbOrder(tbOrder);
        orderInfo.setTbOrderShipping(tbOrderShipping);
        Long b =orderFeign.insertOrder(orderInfo);
        if (b !=null){
            return Result.ok(b);
        }
        return Result.error("error");
    }

}
